import { findAllcategory } from '@/apis/category'
import { topCategory } from '@/apis/constants'
export default {
  namespaced: true,
  state () {
    return {
      list: topCategory.map((item) => ({
        name: item
      }))
    }
  },
  mutations: {
    setCategory (state, payload) {
      state.list = payload
    },
    // 二级类目显示
    open (state, id) {
      const item = state.list.find((item) => item.id === id)
      item.open = true
    },
    // 二级类目隐藏
    hidden (state, id) {
      const item = state.list.find((item) => item.id === id)
      item.open = false
    }
  },
  actions: {
    async setCategoryasync (ctx) {
      const data = await findAllcategory()
      // 控制二级类目是否显示
      data.result.forEach((item) => {
        item.open = false
      })
      ctx.commit('setCategory', data.result)
    }
  }
}
